242D - Dispute - CodeForces Solution


dfs and similar graphs greedy *2100

Please click on ads to support us..

Python Code:



from collections import defaultdict, deque

II = lambda : int(input())
MII = lambda : map(int, input().split())

n, m = MII()
adj = defaultdict(list)
for _ in range(m):
    u, v = MII()
    adj[u - 1].append(v - 1)
    adj[v - 1].append(u - 1)

a = list(MII())
q = deque()
st = [0] * n 
cnt = [0] * n 
for i, [x, y] in enumerate(zip(cnt, a)):
    if x == y: q.append(i)

while len(q):
    v = q.popleft()
    st[v] = 1
    cnt[v] += 1 
    for nxt in adj[v]: 
        cnt[nxt] += 1 
        if cnt[nxt] == a[nxt] and st[nxt] == 0: 
            st[nxt] = 1
            q.append(nxt) 

if len(q): print(-1)
else:
    print(sum(st))
    print(*[i + 1 for i, x in enumerate(st) if x])


Comments

Submit
0 Comments
More Questions

1475B - New Year's Number
461A - Appleman and Toastman
320B - Ping-Pong (Easy Version)
948A - Protect Sheep
387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag
1056A - Determine Line
670B - Game of Robots
1418C - Mortal Kombat Tower
1382B - Sequential Nim
1272C - Yet Another Broken Keyboard
808A - Lucky Year
1245A - Good ol' Numbers Coloring
58B - Coins
1041C - Coffee Break
507A - Amr and Music
1041D - Glider